package GUI;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.border.EtchedBorder;

import Calc.Calculator;

public class GUI {

	private static Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
	final int SHORTCUT_MASK = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
	private Calculator calc = new Calculator();
	private JFrame frame;
	private JTextField humDes, humCru, humSco, humBom, humFac, humTimeDes, humTimeCru, humTimeSco, humTimeBom, humTimeFac, humFP;
	private JTextField azDes, azCru, azSco, azBom, azFac, azTimeDes, azTimeCru, azTimeSco, azTimeBom, azTimeFac, azFP;
	private JTextField xilDes, xilCru, xilSco, xilBom, xilFac, xilTimeDes, xilTimeCru, xilTimeSco, xilTimeBom, xilTimeFac, xilFP;
	private JTextField upkeep, carry, FP;
	private JPanel bigPanel;
	List<JTextField> input;
	List<JTextField> output;
	
	public GUI() {
		makeFrame();		
	}

	private void makeFrame() {
		frame = new JFrame("Hyperium Calculator - By Tezzeret");
		frame.setSize(768, 397);
		frame.setLocation((int)(screenSize.getWidth() - 768) / 2, (int)(screenSize.getHeight() - 397) / 2);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setResizable(false);
		frame.setLayout(new BorderLayout());
		JPanel titlePanel = new JPanel();
		titlePanel.setLayout(new GridLayout(1, 0));
		titlePanel.setBorder(new EtchedBorder());
		JPanel tPanel = new JPanel();
		tPanel.setBorder(new EtchedBorder());
		JLabel label = new JLabel("Human");
		tPanel.add(label);
		titlePanel.add(tPanel);
		tPanel = new JPanel();
		tPanel.setBorder(new EtchedBorder());
		label = new JLabel("Azterk");
		tPanel.add(label);
		titlePanel.add(tPanel);
		tPanel = new JPanel();
		tPanel.setBorder(new EtchedBorder());
		label = new JLabel("Xillor");
		tPanel.add(label);
		titlePanel.add(tPanel);
		frame.add(titlePanel, BorderLayout.NORTH);
		makeMenuBar();
		makeComponents();
		makeLists();
		frame.setVisible(true);			
	}

	private void makeMenuBar() {
		JMenuBar mb = new JMenuBar();
		JMenu menu;
		JMenuItem mi;

		menu = new JMenu("File");
		mi = new JMenuItem("Exit");
		mi.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
		mi.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, SHORTCUT_MASK));
		menu.add(mi);
		mb.add(menu);

		menu = new JMenu("Help");
		mi = new JMenuItem("Help");
		mi.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JOptionPane.showConfirmDialog(frame, "Please report any problems at\n \"leshracshomepage@gmail.com\"", "Help", JOptionPane.CLOSED_OPTION);
			}
		});
		mi.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F1, 0));
		menu.add(mi);
		mi = new JMenuItem("About");
		mi.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				JOptionPane.showConfirmDialog(frame, "Made by: Tezzeret\nSC9, Vikings [-V-]", "About Hyperium Calculator", JOptionPane.CLOSED_OPTION);
			}
		});
		menu.add(mi);
		mb.add(menu);

		frame.setJMenuBar(mb);
	}

	private void makeComponents() {	
		bigPanel = new JPanel();
		bigPanel.setLayout(new GridLayout(0, 3));
		//Human
		JPanel panel = new JPanel();
		panel.setLayout(new GridLayout(0, 1));
		panel.setBorder(new EtchedBorder());
		//Human Destroyers
		JPanel innerPanel = new JPanel();
		innerPanel.setLayout(new GridLayout(1, 0));
		JLabel label = new JLabel("Destroyers:");
		innerPanel.add(label);
		humDes = new JTextField("0");
		innerPanel.add(humDes);
		panel.add(innerPanel);
		//Human Cruisers
		innerPanel = new JPanel();
		innerPanel.setLayout(new GridLayout(1, 0));
		label = new JLabel("Cruisers:");
		innerPanel.add(label);
		humCru = new JTextField("0");
		innerPanel.add(humCru);
		panel.add(innerPanel);
		//Human Scouts
		innerPanel = new JPanel();
		innerPanel.setLayout(new GridLayout(1, 0));
		label = new JLabel("Scouts:");
		innerPanel.add(label);
		humSco = new JTextField("0");
		innerPanel.add(humSco);
		panel.add(innerPanel);
		//Human Bombers
		innerPanel = new JPanel();
		innerPanel.setLayout(new GridLayout(1, 0));
		label = new JLabel("Bombers:");
		innerPanel.add(label);
		humBom = new JTextField("0");
		innerPanel.add(humBom);
		panel.add(innerPanel);
		//Human Factories
		innerPanel = new JPanel();
		innerPanel.setLayout(new GridLayout(1, 0));
		label = new JLabel("Factories:");
		innerPanel.add(label);
		humFac = new JTextField("0");
		innerPanel.add(humFac);
		panel.add(innerPanel);
		bigPanel.add(panel);
		//Azterk
		panel = new JPanel();
		panel.setLayout(new GridLayout(0, 1));
		panel.setBorder(new EtchedBorder());
		//Azterk Destroyers
		innerPanel = new JPanel();
		innerPanel.setLayout(new GridLayout(1, 0));
		label = new JLabel("Destroyers:");
		innerPanel.add(label);
		azDes = new JTextField("0");
		innerPanel.add(azDes);
		panel.add(innerPanel);
		//Azterk Cruisers
		innerPanel = new JPanel();
		innerPanel.setLayout(new GridLayout(1, 0));
		label = new JLabel("Cruisers:");
		innerPanel.add(label);
		azCru = new JTextField("0");
		innerPanel.add(azCru);
		panel.add(innerPanel);
		//Azterk Scouts
		innerPanel = new JPanel();
		innerPanel.setLayout(new GridLayout(1, 0));
		label = new JLabel("Scouts:");
		innerPanel.add(label);
		azSco = new JTextField("0");
		innerPanel.add(azSco);
		panel.add(innerPanel);
		//Azterk Bombers
		innerPanel = new JPanel();
		innerPanel.setLayout(new GridLayout(1, 0));
		label = new JLabel("Bombers:");
		innerPanel.add(label);
		azBom = new JTextField("0");
		innerPanel.add(azBom);
		panel.add(innerPanel);
		//Azterk Factories
		innerPanel = new JPanel();
		innerPanel.setLayout(new GridLayout(1, 0));
		label = new JLabel("Factories:");
		innerPanel.add(label);
		azFac = new JTextField("0");
		innerPanel.add(azFac);
		panel.add(innerPanel);
		bigPanel.add(panel);
		//Xillor
		panel = new JPanel();
		panel.setLayout(new GridLayout(0, 1));
		panel.setBorder(new EtchedBorder());
		//Xillor Destroyers
		innerPanel = new JPanel();
		innerPanel.setLayout(new GridLayout(1, 0));
		label = new JLabel("Destroyers:");
		innerPanel.add(label);
		xilDes = new JTextField("0");
		innerPanel.add(xilDes);
		panel.add(innerPanel);
		//Xillor Cruisers
		innerPanel = new JPanel();
		innerPanel.setLayout(new GridLayout(1, 0));
		label = new JLabel("Cruisers:");
		innerPanel.add(label);
		xilCru = new JTextField("0");
		innerPanel.add(xilCru);
		panel.add(innerPanel);
		//Xillor Scouts
		innerPanel = new JPanel();
		innerPanel.setLayout(new GridLayout(1, 0));
		label = new JLabel("Scouts:");
		innerPanel.add(label);
		xilSco = new JTextField("0");
		innerPanel.add(xilSco);
		panel.add(innerPanel);
		//Xillor Bombers
		innerPanel = new JPanel();
		innerPanel.setLayout(new GridLayout(1, 0));
		label = new JLabel("Bombers:");
		innerPanel.add(label);
		xilBom = new JTextField("0");
		innerPanel.add(xilBom);
		panel.add(innerPanel);
		//Xillor Factories
		innerPanel = new JPanel();
		innerPanel.setLayout(new GridLayout(1, 0));
		label = new JLabel("Factories:");
		innerPanel.add(label);
		xilFac = new JTextField("0");
		innerPanel.add(xilFac);
		panel.add(innerPanel);
		bigPanel.add(panel);
		makeComponents2();
	}
	
	private void makeComponents2() {
		//Human
		JPanel panel = new JPanel();
		panel.setLayout(new GridLayout(0, 1));
		panel.setBorder(new EtchedBorder());
		//Human Destroyers
		JPanel innerPanel = new JPanel();
		innerPanel.setLayout(new GridLayout(1, 0));
		JLabel label = new JLabel("Destroyers Buildtime: ");
		innerPanel.add(label);
		humTimeDes = new JTextField("0");
		humTimeDes.setEditable(false);
		innerPanel.add(humTimeDes);
		panel.add(innerPanel);
		//Human Cruisers
		innerPanel = new JPanel();
		innerPanel.setLayout(new GridLayout(1, 0));
		label = new JLabel("Cruisers Buildtime:");
		innerPanel.add(label);
		humTimeCru = new JTextField("0");
		humTimeCru.setEditable(false);
		innerPanel.add(humTimeCru);
		panel.add(innerPanel);
		//Human Scouts
		innerPanel = new JPanel();
		innerPanel.setLayout(new GridLayout(1, 0));
		label = new JLabel("Scouts Buildtime:");
		innerPanel.add(label);
		humTimeSco = new JTextField("0");
		humTimeSco.setEditable(false);
		innerPanel.add(humTimeSco);
		panel.add(innerPanel);
		//Human Bombers
		innerPanel = new JPanel();
		innerPanel.setLayout(new GridLayout(1, 0));
		label = new JLabel("Bombers Buildtime:");
		innerPanel.add(label);
		humTimeBom = new JTextField("0");
		humTimeBom.setEditable(false);
		innerPanel.add(humTimeBom);
		panel.add(innerPanel);
		//Human Factories
		innerPanel = new JPanel();
		innerPanel.setLayout(new GridLayout(1, 0));
		label = new JLabel("Factories in 1 hour:");
		innerPanel.add(label);
		humTimeFac = new JTextField("0");
		humTimeFac.setEditable(false);
		innerPanel.add(humTimeFac);
		panel.add(innerPanel);
		bigPanel.add(panel);
		//Azterk
		panel = new JPanel();
		panel.setLayout(new GridLayout(0, 1));
		panel.setBorder(new EtchedBorder());
		//Azterk Destroyers
		innerPanel = new JPanel();
		innerPanel.setLayout(new GridLayout(1, 0));
		label = new JLabel("Destroyers Buildtime: ");
		innerPanel.add(label);
		azTimeDes = new JTextField("0");
		azTimeDes.setEditable(false);
		innerPanel.add(azTimeDes);
		panel.add(innerPanel);
		//Azterk Cruisers
		innerPanel = new JPanel();
		innerPanel.setLayout(new GridLayout(1, 0));
		label = new JLabel("Cruisers Buildtime:");
		innerPanel.add(label);
		azTimeCru = new JTextField("0");
		azTimeCru.setEditable(false);
		innerPanel.add(azTimeCru);
		panel.add(innerPanel);
		//Azterk Scouts
		innerPanel = new JPanel();
		innerPanel.setLayout(new GridLayout(1, 0));
		label = new JLabel("Scouts Buildtime:");
		innerPanel.add(label);
		azTimeSco = new JTextField("0");
		azTimeSco.setEditable(false);
		innerPanel.add(azTimeSco);
		panel.add(innerPanel);
		//Azterk Bombers
		innerPanel = new JPanel();
		innerPanel.setLayout(new GridLayout(1, 0));
		label = new JLabel("Bombers Buildtime:");
		innerPanel.add(label);
		azTimeBom = new JTextField("0");
		azTimeBom.setEditable(false);
		innerPanel.add(azTimeBom);
		panel.add(innerPanel);
		//Azterk Factories
		innerPanel = new JPanel();
		innerPanel.setLayout(new GridLayout(1, 0));
		label = new JLabel("Factories in 1 hour:");
		innerPanel.add(label);
		azTimeFac = new JTextField("0");
		azTimeFac.setEditable(false);
		innerPanel.add(azTimeFac);
		panel.add(innerPanel);
		bigPanel.add(panel);
		//Xillor
		panel = new JPanel();
		panel.setLayout(new GridLayout(0, 1));
		panel.setBorder(new EtchedBorder());
		//Xillor Destroyers
		innerPanel = new JPanel();
		innerPanel.setLayout(new GridLayout(1, 0));
		label = new JLabel("Destroyers Buildtime: ");
		innerPanel.add(label);
		xilTimeDes = new JTextField("0");
		xilTimeDes.setEditable(false);
		innerPanel.add(xilTimeDes);
		panel.add(innerPanel);
		//Xillor Cruisers
		innerPanel = new JPanel();
		innerPanel.setLayout(new GridLayout(1, 0));
		label = new JLabel("Cruisers Buildtime:");
		innerPanel.add(label);
		xilTimeCru = new JTextField("0");
		xilTimeCru.setEditable(false);
		innerPanel.add(xilTimeCru);
		panel.add(innerPanel);
		//Xillor Scouts
		innerPanel = new JPanel();
		innerPanel.setLayout(new GridLayout(1, 0));
		label = new JLabel("Scouts Buildtime:");
		innerPanel.add(label);
		xilTimeSco = new JTextField("0");
		xilTimeSco.setEditable(false);
		innerPanel.add(xilTimeSco);
		panel.add(innerPanel);
		//Xillor Bombers
		innerPanel = new JPanel();
		innerPanel.setLayout(new GridLayout(1, 0));
		label = new JLabel("Bombers Buildtime:");
		innerPanel.add(label);
		xilTimeBom = new JTextField("0");
		xilTimeBom.setEditable(false);
		innerPanel.add(xilTimeBom);
		panel.add(innerPanel);
		//Xillor Factories
		innerPanel = new JPanel();
		innerPanel.setLayout(new GridLayout(1, 0));
		label = new JLabel("Factories in 1 hour:");
		innerPanel.add(label);
		xilTimeFac = new JTextField("0");
		xilTimeFac.setEditable(false);
		innerPanel.add(xilTimeFac);
		panel.add(innerPanel);
		bigPanel.add(panel);
		makeComponents3();
	}
	
	private void makeComponents3() {
		//Upkeep
		JPanel panel = new JPanel();
		panel.setLayout(new GridLayout(0, 1));
		panel.setBorder(new EtchedBorder());
		JPanel innerPanel = new JPanel();
		innerPanel.setLayout(new GridLayout(1, 0));
		JLabel label = new JLabel("Upkeep:");
		innerPanel.add(label);
		upkeep = new JTextField("0");
		upkeep.setEditable(false);
		innerPanel.add(upkeep);
		panel.add(innerPanel);
		//Carry Capacity
		innerPanel = new JPanel();
		innerPanel.setLayout(new GridLayout(1, 0));
		label = new JLabel("Carry Capacity:");
		innerPanel.add(label);
		carry = new JTextField("0");
		carry.setEditable(false);
		innerPanel.add(carry);
		panel.add(innerPanel);
		//Fleet Points
		innerPanel = new JPanel();
		innerPanel.setLayout(new GridLayout(1, 0));
		label = new JLabel("Fleet Points:");
		innerPanel.add(label);
		FP = new JTextField("0");
		FP.setEditable(false);
		innerPanel.add(FP);
		panel.add(innerPanel);
		bigPanel.add(panel);
		//Fleet Points Breakdown
		panel = new JPanel();
		panel.setLayout(new GridLayout(0, 1));
		panel.setBorder(new EtchedBorder());
		//Human
		innerPanel = new JPanel();
		innerPanel.setLayout(new GridLayout(1, 0));
		label = new JLabel("Human Fleet Points:");
		innerPanel.add(label);
		humFP = new JTextField("0");
		humFP.setEditable(false);
		innerPanel.add(humFP);
		panel.add(innerPanel);
		//Azterk
		innerPanel = new JPanel();
		innerPanel.setLayout(new GridLayout(1, 0));
		label = new JLabel("Azterk Fleet Points:");
		innerPanel.add(label);
		azFP = new JTextField("0");
		azFP.setEditable(false);
		innerPanel.add(azFP);
		panel.add(innerPanel);
		//Xillor
		innerPanel = new JPanel();
		innerPanel.setLayout(new GridLayout(1, 0));
		label = new JLabel("Xillor Fleet Points:");
		innerPanel.add(label);
		xilFP = new JTextField("0");
		xilFP.setEditable(false);
		innerPanel.add(xilFP);
		panel.add(innerPanel);		
		bigPanel.add(panel);
		//calculate button
		panel = new JPanel();
		panel.setLayout(new GridLayout(0, 1));
		panel.setBorder(new EtchedBorder());
		JButton button = new JButton("Calculate");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				calculate();
			}
		});
		panel.add(button);
		bigPanel.add(panel);
		frame.add(bigPanel, BorderLayout.CENTER);
	}
	
	private void makeLists() {
		input = new ArrayList<JTextField>();
		output = new ArrayList<JTextField>();
		input.add(humDes);
		input.add(humCru);
		input.add(humSco);
		input.add(humBom);
		input.add(humFac);
		input.add(azDes);
		input.add(azCru);
		input.add(azSco);
		input.add(azBom);
		input.add(azFac);
		input.add(xilDes);
		input.add(xilCru);
		input.add(xilSco);
		input.add(xilBom);
		input.add(xilFac);
		output.add(humTimeDes);
		output.add(humTimeCru);
		output.add(humTimeSco);
		output.add(humTimeBom);
		output.add(humTimeFac);
		output.add(humFP);
		output.add(azTimeDes);
		output.add(azTimeCru);
		output.add(azTimeSco);
		output.add(azTimeBom);
		output.add(azTimeFac);
		output.add(azFP);
		output.add(xilTimeDes);
		output.add(xilTimeCru);
		output.add(xilTimeSco);
		output.add(xilTimeBom);
		output.add(xilTimeFac);
		output.add(xilFP);
		output.add(upkeep);
		output.add(carry);
		output.add(FP);
	}
	
	private void calculate() {
		String inputData = "";
		for(JTextField field : input) {
			if(field != xilFac)
				inputData += "" + field.getText() + ":";
			else
				inputData += "" + field.getText();
		}
		String outputData = calc.calculate(inputData);
		String[] fields = outputData.split(":");
		int number = 0;
		for(String str : fields) {
			output.get(number).setText(str);
			number++;
		}		
	}	
}
